﻿using System;
using System.Data;
using System.Collections.Generic;
using eSYSU.Data.DataAccess;
using eSYSU.Web.Interface;

namespace eSYSU.Web.Business
{
	/// <summary>
	/// 用户的业务逻辑方法封装
	/// </summary>
	public class UserService : IUser
	{
		public UserService()
		{
			//
			//TODO: 在此处添加构造函数逻辑
			//
		}

		#region 管理员登录验证
		/// <summary>
		/// 管理员登录验证
		/// </summary>
		/// <param name="sUserName">用户名</param>
		/// <param name="sPassWord">登录口令</param>
		/// <returns>验证结果</returns>
		public bool LoginAdmin(string sUserName, string sPassWord)
		{
			bool succ = false;
			string sMD5;

			sMD5 = eSYSU.Web.Common.StringHelper.MD5(sPassWord.Trim());

			DBO myDBO = new DBO();
			string sSQL = "select * from Admin where AdminName='" + sUserName.Trim() + "' and AdminPassword='" + sMD5 + "'";
			if (myDBO.ExecSQLDirectly(sSQL))
			{
				succ = true;
			}
			return succ;
		}
		#endregion

		/// <summary>
		/// 用户登录验证
		/// </summary>
		/// <param name="sUserName">用户名</param>
		/// <param name="sPassWord">登录口令</param>
		/// <returns>验证结果</returns>
		public static bool UserLogin(string sUserName, string sPassWord)
		{
			bool succ = false;

			sPassWord = eSYSU.Web.Common.StringHelper.MD5(sPassWord.Trim());

			IDictionary<string, object> parmsDic = new Dictionary<string, object>();
			parmsDic.Add("@un", sUserName.Trim().ToLower());
			parmsDic.Add("@pw", sPassWord);

			if (SqlHelper.GetDataTable(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_User_SignIn", parmsDic).Rows.Count > 0)
			{
				succ = true;
			}
			return succ;
		}

		#region 新用户注册
		/// <summary>
		/// 插入一个新用户
		/// </summary>
		/// <param name="sUserName">用户名</param>
		/// <param name="sPassWord">登录口令</param>
		/// <returns>验证结果</returns>
		public bool AddUser(string sUserName, string sPassWord)
		{
			bool succ = false;
			string sMD5;

			sMD5 = eSYSU.Web.Common.StringHelper.MD5(sPassWord.Trim());

			DBO myDBO = new DBO();
			string sSQL = "select * from Admin where AdminName='" + sUserName.Trim() + "' and AdminPassword='" + sMD5 + "'";
			if (myDBO.ExecSQLDirectly(sSQL))
			{
				succ = true;
			}
			return succ;
		}
		#endregion

		/// <summary>
		/// 识别身份及权限
		/// </summary>
		/// <param name="sPageName">调用本方法的页面名称</param>
		/// <returns>用户名</returns>
		public string UserIdentification(string sPageName)
		{
			return "";
		}



		/// <summary>
		/// 创建一个新用户
		/// </summary>
		/// <returns>创建是否成功</returns>
		public bool CreateNewUser()
		{
			string sSql = "Insert into tb_User(UserName,Password) Values()";

			DBO dbo = new DBO();
			if (dbo.ExecSQLDirectly(sSql))
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 建立一个用户登录会话
		/// </summary>
		/// <param name="username">用户名</param>
		/// <param name="timeout">超时时间</param>
		public static void SetUserLoginSession(string username, int timeout)
		{
			eSYSU.Web.Common.LocalStorage.SetSession("eSYSU_Session_UserLogin", username, 30);
		}

		/// <summary>
		/// 获取当前用户登录会话的状态
		/// </summary>
		/// <returns>会话状态</returns>
		public static bool IsLogin()
		{
			return eSYSU.Web.Common.LocalStorage.CheckSession("eSYSU_Session_UserLogin");
		}

		/// <summary>
		/// 获取学院列表
		/// </summary>
		/// <returns>包含学院列表的DataTable对象</returns>
		public static DataTable GetCollegeList()
		{
			return Data.DataAccess.SqlHelper.GetDataTable(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_College_Get", new Dictionary<string, object>());
		}
	}
}